<?php

class AskModel extends Model {
	public $table = '360_ask';

	public function addAsk(){
		if(empty($_SESSION['id'])){
			return __WEB__.'?m=Index&c=User&a=login';
		}
		$_POST['ask_uid'] = $_SESSION['id'];
		$_POST['ask_time'] = time();
		$User = new Model('360_user');
		$Field = $User->where("uid = {$_SESSION['id']}")->find();
		if($Field['golds']<$_POST['ask_gold']){
			$this->error = '您的金币不够';
			return false;
		}
		$Sql = "UPDATE 360_user SET golds=golds-{$_POST['ask_gold']} WHERE uid=".$Field['uid'];
		$this->exe($Sql);
		return $this->insert($_POST);
	}

	public function AskList(){
		return $this->select();
	}
	public function getUser(){
		$User = new Model('360_user');
		$Field = $User->where("uid = {$_SESSION['id']}")->find();
		return $Field;
	}

	// 查询所有的问题
	public function AllAsk(){
		$Count = $this->count();
		$Page = new Page($Count,10);
		$PageList = $Page->show_b();
		$Limit = $Page->limit();
		$Sql = "SELECT
		ask.ask_id,
		COUNT(*) AS count,
		ask.ask_content,ask.ask_gold,
		ask.ask_time,
		ans.ans_content
		FROM
		360_ask AS ask
		LEFT JOIN 360_ans AS ans ON ask.ask_id = ans.ask_id
		GROUP BY ask.ask_id  ORDER BY ask.ask_id DESC LIMIT {$Limit}";
		 $AskList = $this->query($Sql);
		return array('AskList'=>$AskList,'Pages'=>$PageList,'count'=>$Count);
	}

	public function WhiteAsk(){
		// 待解决问题列表SQL
		$Sql = " SELECT ask.ask_id,ask_content,ask_time,ask_gold,cid FROM 360_ask AS ask 
		LEFT JOIN 360_accept AS accept
		ON ask.ask_id = accept.ask_id
		WHERE ask.state = 0
		GROUP BY ask.ask_id";
		return $this->query($Sql);
	}

	public function SettleAsk(){
		// 条数
		$CSql = "SELECT count(*)AS c FROM 360_ask AS ask 
		INNER JOIN 360_accept AS accept
		ON ask.ask_id = accept.ask_id WHERE
		ask.ask_id IS NOT NULL";
		$Counts = $this->query($CSql);
		$Count = $Counts[0]['c'];
		// 实例化分页类
		$Page = new Page($Count,10);
		// 页码方法
		$PageList = $Page->show_b();
		// LIMIT 条数
		$Limit = $Page->limit();
		// 已决问题列表SQL
		$Sql = " SELECT * FROM 360_ask AS ask
		INNER JOIN 360_ans AS ans 
		ON ask.ask_id = ans.ask_id INNER JOIN 360_accept AS accept 
		ON ask.ask_id = accept.ask_id
		GROUP BY ask.ask_id  ORDER BY ask.ask_id DESC LIMIT {$Limit}";
		 $AskList = $this->query($Sql);
		 return array('count'=>$Count,'Pages'=>$PageList,'AskList'=>$AskList);
	}
	public function NoAnswerAsk(){
		// 条数
		$CSql = "SELECT count(*) AS c
		FROM 360_ask AS ask 
		LEFT JOIN 360_ans AS ans 
		ON ask.ask_id = ans.ask_id
		WHERE ans.ans_id IS NULL;";
		$Counts = $this->query($CSql);
		$Count = $Counts[0]['c'];
		// 实例化分页类
		$Page = new Page($Count,10);
		// 页码方法
		$PageList = $Page->show_b();
		// LIMIT 条数
		$Limit = $Page->limit();
		// 没有回答的问题列表SQL
		$Sql = " SELECT ask_content,ask_time,ask_gold
		FROM 360_ask AS ask 
		LEFT JOIN 360_ans AS ans 
		ON ask.ask_id = ans.ask_id
		WHERE ans.ans_id IS NULL  ORDER BY ask.ask_id DESC LIMIT {$Limit}";

		 $AskList = $this->query($Sql);
		 return array('count'=>$Count,'Pages'=>$PageList,'AskList'=>$AskList);
	}


	public function WhiteAskGolds(){
		// 待解决问题列表SQL
		$Sql = "SELECT
		ask.ask_id,
		COUNT(*) AS count,
		ask.ask_content,ask.ask_gold,
		ask.ask_time,
		ans.ans_content
		FROM
		360_ask AS ask
		LEFT JOIN 360_ans AS ans ON ask.ask_id = ans.ask_id
		GROUP BY ask.ask_id  ORDER BY ask.ask_gold DESC ";
		 $AskList = $this->query($Sql);
		 return $AskList;
	}

	public function getList(){
		// 分类操作句柄
		$DB = new Model('360_categorys');
		$cid = isset($_GET['cid'])?$_GET['cid']:'all';
		// 问题操作句柄
		$A = new Model('360_ask');
		if($cid!='all'){
			// 获取当前被点击的那条分类相关的记录
			$Find = $DB->where("cid = {$cid} ")->find();
			// 获取跟上面的分类相同顶级分类的记录
			$Field = $DB->where("pid = {$Find['pid']}")->select();
			// 顶级分类
			$Top = $DB->where("pid = {$Find['pid']}")->group("pid")->find();
			// 获取跟当前分类相关的问题列表
			$AskList = $A->where("cid = {$cid} ")->select();
			// 返回相关的列表记录
			return array('Field'=>$Field,'top'=>$Top,'Find'=>$Find,'Ask',$AskList);
		}
		if($cid=='all'){
			// 点击全部分类并且cid不存在时的分类记录
			$List = $DB->group("pid")->select();
			// 点击全部分类并且cid不存在时的问题列表记录
			$AskList = $A->select();
			return array('Field'=>$List,'Ask'=>$AskList);
		}
	}
}

?>